package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadController {
    @RequestMapping("upload")
    public String upload(@RequestParam("uploadImg")MultipartFile mf) throws IOException {
        //得到上传文件的名称
        var fileName = mf.getOriginalFilename();

        if (fileName.endsWith(".jpg")||fileName.endsWith(".png")) {

            fileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
            //将上传文件以二进制数据写入指定文件。
            mf.transferTo(new File("C:\\Users\\陈\\Desktop\\img/" + fileName));
            return fileName;
        }
        return "no";
    }
}
